#include "dummyrenderer.h"

#include "assist.h"

#include <QOpenGLContext>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>

#include <QDebug>

DummyRenderer::DummyRenderer(QWindow* window)
    : Renderer("dummy-path", window)
{
}

void DummyRenderer::initialize()
{
    qDebug() << "DummyRenderer::initialize()";

    m_vao = new QOpenGLVertexArrayObject(this);

    if (!m_vao->create()) {
        qFatal("QOpenGLVertexArrayObject creation failed.");
    }

    QOpenGLVertexArrayObject::Binder binder(m_vao);
    Q_UNUSED(binder)

    m_program = new QOpenGLShaderProgram(this);
    initializeProgramOrFail(m_program, ":/shaders/vertex_1.vert", ":/shaders/fragment_1.fsh");

    float vertexPosData []  = {
        0.0f,  0.0f,  0.0f,
        0.5f, -0.5f,  0.0f,
        0.5f,  0.0f,  0.0f,
    };

    m_vertexPosBuffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);

    if (!m_vertexPosBuffer.create())
        qFatal("QOpenGLBuffer::create() failed.");

    m_vertexPosBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_vertexPosBuffer.bind();
    m_vertexPosBuffer.allocate(vertexPosData, 3*3*sizeof(float));

    m_program->bind();
    m_program->enableAttributeArray("vertex_pos");
    m_program->setAttributeBuffer("vertex_pos", GL_FLOAT, 0, 3);
}

void DummyRenderer::doRender()
{
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

     glDisable(GL_DEPTH_TEST);

    QOpenGLVertexArrayObject::Binder binder(m_vao);
    Q_UNUSED(binder)

    glDrawArrays(GL_TRIANGLES, 0, 3);
}
